package com.suo.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Mod;
import com.suo.pojo.ProductInfo;
import com.suo.pojo.vo.ProductInfoVo;
import com.suo.service.ProductInfoService;
import com.suo.utils.FileNameUtil;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/prod")
public class ProductInfoAction {

    private ProductInfoService productInfoService;

    private String saveFileName = "";

    @Autowired
    public void setProductInfoService(ProductInfoService productInfoService) {
        this.productInfoService = productInfoService;
    }

    @RequestMapping("/getAll")
    public ModelAndView getAll() {
        ModelAndView modelAndView = new ModelAndView();
        List<ProductInfo> productInfos = productInfoService.getAll();
        modelAndView.addObject("list",productInfos);
        modelAndView.setViewName("product");
        return modelAndView;
    }

    @RequestMapping("/split")
    public ModelAndView split() {
        ModelAndView modelAndView = new ModelAndView();
        PageInfo pageInfo = productInfoService.split(1, 5);
        modelAndView.addObject("info",pageInfo);
        modelAndView.setViewName("product");
        return modelAndView;
    }

    @RequestMapping("/ajaxSplit")
    @ResponseBody
    public void ajaxSplit(int page, HttpSession session) {
        PageInfo pageInfo = productInfoService.split(page, 5);
        session.setAttribute("info",pageInfo);
    }

    @RequestMapping("/ajaxImg")
    @ResponseBody
    public Object ajaxImg(MultipartFile pimage, HttpServletRequest request) {

        //提取工具类生成文件名+上传图片后缀
        saveFileName = FileNameUtil.getUUIDFileName() + FileNameUtil.getFileType(pimage.getOriginalFilename());

        //得到项目中图片的存储路径
        String path = request.getServletContext().getRealPath("/image_big");

        //转存到项目路径path中
        try {
            pimage.transferTo(new File(path + File.separator + saveFileName));
        } catch (IOException e) {
            e.printStackTrace();
        }

        //返回客户端JSON对象，封装图片路径，为了在页面实现立即回显
        JSONObject object = new JSONObject();
        object.put("imgurl",saveFileName);

        return object.toString();
    }

    @RequestMapping("/save")
    public ModelAndView save(ProductInfo info) {
        ModelAndView modelAndView = new ModelAndView();
        info.setpImage(saveFileName);
        info.setpDate(new Date());
        int save = productInfoService.save(info);

        if(save > 0) {
            modelAndView.addObject("msg","添加成功!!!");
        }
        else {
            modelAndView.addObject("msg","添加失败!!!");
        }
        modelAndView.setViewName("redirect:/prod/split.action");

        return modelAndView;
    }

    @RequestMapping("/one")
    public ModelAndView one(int pid,int page) {
        //System.out.println(vo);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("update");
        ProductInfo one = productInfoService.one(pid);
        modelAndView.addObject("prod",one);
        modelAndView.addObject("page",page);
        //modelAndView.addObject("vo",vo);
        return modelAndView;
    }

//    @RequestMapping("/update")
//    public ModelAndView update(ProductInfo info) throws ServletException, IOException {
//        ModelAndView modelAndView = new ModelAndView();
//        info.setpImage(saveFileName);
//        info.setpDate(new Date());
//        int i = productInfoService.update(info);
//        if(i > 0) {
//            modelAndView.addObject("msg","提交成功!!!");
//        }
//        else {
//            modelAndView.addObject("msg","提交失败!!!");
//        }
//
//        modelAndView.setViewName("forward:/prod/ajaxSplit.action");
//        return modelAndView;
//    }

    @RequestMapping("/update")
    public ModelAndView update(ProductInfo info,HttpSession session) throws ServletException, IOException {
        ModelAndView modelAndView = new ModelAndView();
        info.setpImage(saveFileName);
        info.setpDate(new Date());
        int i = productInfoService.update(info);
        if(i > 0) {
            modelAndView.addObject("msg","提交成功!!!");
        }
        else {
            modelAndView.addObject("msg","提交失败!!!");
        }

        ProductInfoVo vo = (ProductInfoVo) session.getAttribute("vo");

        if(vo == null) {
            vo = new ProductInfoVo();
        }

        PageInfo pageInfo = productInfoService.splitCondition(vo, 5);

        session.setAttribute("info",pageInfo);

        modelAndView.setViewName("/product");
        return modelAndView;
    }

    @RequestMapping("/reBack")
    public ModelAndView reBack(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();

        ProductInfoVo vo = (ProductInfoVo) session.getAttribute("vo");

        if(vo == null) {
            vo = new ProductInfoVo();
        }

        PageInfo pageInfo = productInfoService.splitCondition(vo, 5);

        session.setAttribute("info",pageInfo);

        modelAndView.setViewName("/product");
        return modelAndView;
    }

//    @RequestMapping("/delete")
//    public ModelAndView delete(int pid,ProductInfoVo vo) throws ServletException, IOException {
//        ModelAndView modelAndView = new ModelAndView();
//        int delete = productInfoService.delete(pid);
//        if(delete > 0) {
//            modelAndView.addObject("msg","删除成功!!!");
//        }
//        else {
//            modelAndView.addObject("msg","删除失败!!!");
//        }
//        modelAndView.setViewName("forward:/prod/splitCondition.action");
//        return modelAndView;
//    }

    @RequestMapping("/delete")
    @ResponseBody
    public void delete(int pid,ProductInfoVo vo,HttpSession session) throws ServletException, IOException {
        int delete = productInfoService.delete(pid);
        PageInfo pageInfo = productInfoService.splitCondition(vo, 5);
        session.setAttribute("info",pageInfo);
    }

    @RequestMapping("/deletebatch")
    @ResponseBody
    public void deletebatch(int[] str,ProductInfoVo vo,HttpSession session) throws ServletException, IOException {
        //ModelAndView modelAndView = new ModelAndView();

        int deletebatch = productInfoService.deletebatch(str);
//        if (deletebatch > 0) {
//            modelAndView.addObject("msg","删除成功!!!");
//        }
//        else {
//            modelAndView.addObject("msg","删除失败!!!");
//        }
        PageInfo pageInfo = productInfoService.splitCondition(vo, 5);
        session.setAttribute("info",pageInfo);

    }

    @RequestMapping("/selectCondition")
    public ModelAndView selectCondition(ProductInfoVo vo) {
        List<ProductInfo> productInfos = productInfoService.selectCondition(vo);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("product");
        modelAndView.addObject("productInfos",productInfos);
        return modelAndView;
    }

    @RequestMapping("/splitCondition")
    @ResponseBody
    public void splitCondition(ProductInfoVo vo, HttpSession session) {
        PageInfo pageInfo = productInfoService.splitCondition(vo, 5);

        session.setAttribute("vo",vo);
        session.setAttribute("info",pageInfo);
    }

}
